package lab_2.windows;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import lab_2.Client;


/**
 * Frame for Authorization
 * @author pacific
 */
public class AuthorizationDialog extends JDialog {

    public AuthorizationDialog( IMWindow window, String oldUsername, String oldPassword ) {
        super( window, true ) ;
        setMainWindow( window ) ;
        setLayout( new GridLayout( 4, 1 ) );

        //1 - message
        JPanel panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) ) ;
        panel.add( getMessage() ) ;
        add( panel ) ;

        //2 line with field for login
        panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) ) ;
        panel.add( new JLabel( "   Логин:" ) ) ;
        panel.add( getUsername() ) ;
        add( panel ) ;
        getUsername().setText( oldUsername ) ;

        //3 line with field for password
        panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) ) ;
        panel.add( new JLabel( "Пароль:" ) ) ;
        panel.add( getPassword() ) ;
        add( panel ) ;
        getPassword().setText( oldPassword ) ;

        //4 - panel with buttons: "Connect", "Exit"
        panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) ) ;

        JButton retry = new JButton( "Войти" ) ;
        retry.addActionListener( new ConnectListener() ) ;
        panel.add( retry ) ;
        JButton exit = new JButton( "Выход из программы" ) ;
        exit.addActionListener( new ExitListener() ) ;
        panel.add( exit ) ;
        add( panel ) ;

        pack() ;
        Point mainWindowLocation = getMainWindow().getLocation() ;
        setLocation( mainWindowLocation ) ;
        setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ) ;
        setResizable( false ) ;
        setVisible( true ) ;
    }


    /** Listener for button CONNECT */
    private class ConnectListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // set messange for next try
            getMessage().setText( ABORTIVELY ) ;
            getMainWindow().getClient().setUsername( getUsername().getText() ) ;
            getMainWindow().getClient().setPassword( "" + getPassword().getText() );
            getMainWindow().getClient().setStateClient( Client.ClientState.CONNECTION ) ;

            dispose() ;
        }
    }


    /** Listener for button EXIT */
    class ExitListener implements ActionListener {
        @Override
        public void actionPerformed (ActionEvent e) {
            dispose() ;
            getMainWindow().exit() ;
        }
    }


    //-----Fields-----

    public static final String FIRST_TIME = "Авторизуйтесь пожалуйста" ;
    public static final String ABORTIVELY = "Не удалось соединиться не с одним из серверов" ;
    private static final int COLUMNS_FOR_FIELDS = 20 ;
    private IMWindow mainWindow ;
    static JLabel message = new JLabel( FIRST_TIME ) ;

    JTextField username = new JTextField(COLUMNS_FOR_FIELDS) ;
    JPasswordField password = new JPasswordField(COLUMNS_FOR_FIELDS) ;


    //-----Getters and Setters-----

    //  for "message"
    private JLabel getMessage() {
        return message;
    }
    // for "username"
    private JTextField getUsername() {
        return username;
    }
    //  for "password"
    private JPasswordField getPassword() {
        return password;
    }
    //  for "mainWindow"
    private IMWindow getMainWindow() {
        return mainWindow;
    }
    private void setMainWindow(IMWindow Window) {
        mainWindow = Window;
    }
}
